package com.artron.mediaartron.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.ui.callback.TitleChangeCallback;
import com.artron.mediaartron.ui.fragment.made.picture.PictureSelectionFragment;
import com.artron.mediaartron.ui.fragment.made.picture.TabFragment;
import com.artron.mediaartron.ui.helper.BoxingGlideLoader;
import com.bilibili.boxing.AbsBoxingActivity;
import com.bilibili.boxing.AbsBoxingViewFragment;
import com.bilibili.boxing.BoxingMediaLoader;
import com.bilibili.boxing.model.entity.BaseMedia;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.fragment;

public class PictureSelectionActivity extends AbsBoxingActivity implements TitleChangeCallback {

    public static final String IS_HAVE_TAB_FRAGMENT = "IsHaveTabFragment";

    static {
        BoxingMediaLoader.getInstance().init(new BoxingGlideLoader());
    }


    private TextView mTvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_general);
        initActionBar();

        initView();
    }

    private void initView() {
        mTvTitle = ((TextView) findViewById(R.id.General_title));
        setTitle(getString(R.string.local_album));
    }

    private void initActionBar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.General_toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_back);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        //TODO 这里的适配还没有做，只是适配了6.0以上的手机，以后来补充
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (actionBar != null) {
//                getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                getWindow().setStatusBarColor(UIUtils.getColor(R.color.white));

                //fitsSystemWindow 为 false, 不预留系统栏位置.
                ViewGroup mContentView = ((ViewGroup) getWindow().findViewById(Window.ID_ANDROID_CONTENT));
                View mChildView = mContentView.getChildAt(0);
                if (mChildView != null) {
                    ViewCompat.setFitsSystemWindows(mChildView, true);
                    ViewCompat.requestApplyInsets(mChildView);
                }
            }
        }
    }

    @NonNull
    @Override
    public AbsBoxingViewFragment onCreateBoxingView(ArrayList<BaseMedia> medias) {
        boolean isHaveTabFragment = getIntent().getBooleanExtra(IS_HAVE_TAB_FRAGMENT, false);

        if (isHaveTabFragment) {
            TabFragment fragment = (TabFragment) getSupportFragmentManager()
                    .findFragmentByTag(TabFragment.TAG);
            if (fragment == null) {
                fragment = TabFragment.newInstance();
                fragment.setSelectedBundle(medias);
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.General_content, fragment, TabFragment.TAG)
                        .commitAllowingStateLoss();
            }

            return fragment.getBoxingFragment();
        } else {
            PictureSelectionFragment fragment = (PictureSelectionFragment) getSupportFragmentManager()
                    .findFragmentByTag(PictureSelectionFragment.TAG);
            if (fragment == null) {
                fragment = PictureSelectionFragment.newInstance(false);
                fragment.setSelectedBundle(medias);
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.General_content, fragment, PictureSelectionFragment.TAG)
                        .commitAllowingStateLoss();
            }

            return fragment;
        }
    }

    @Override
    public void onBoxingFinish(Intent intent, @Nullable List<BaseMedia> medias) {
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.picture_selection, menu);
        return true;
    }

    @Override
    public void setTitle(String title) {
        if (mTvTitle != null) {
            mTvTitle.setText(title);
        }
    }


}
